//
//  XDSelSecureLineView.m
//  DAU-iOS
//
//  Created by XiangDongMBP on 2016/12/7.
//  Copyright © 2016年 East. All rights reserved.
//

#import "XDSelSecureLineView.h"

@interface XDSelSecureLineView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIView *bottomBgView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, assign) NSInteger lowerValue;
@property (nonatomic, assign) NSInteger upperValue;

@end

static const NSInteger kMaxValue = 100;
static const NSInteger kMinValue = -50;
static const CGFloat kBottomBgViewHeight = 250.0;

@implementation XDSelSecureLineView

- (instancetype)initWithLower:(NSInteger)lower upper:(NSInteger)upper delegate:(id<XDSelSecureLineViewDelegate>)delegate {
    self = [self init];
    if (self) {
        self.lowerValue = (lower < kMinValue || lower > kMaxValue) ? kMinValue : lower;
        self.upperValue = (upper > kMaxValue || upper < kMinValue) ? kMaxValue : upper;
        self.delegate = delegate;
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.alpha = 0;
        
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat height = [UIScreen mainScreen].bounds.size.height;
        self.bottomBgView = [[UIView alloc] initWithFrame:CGRectMake(0, height, width, kBottomBgViewHeight)];
        self.bottomBgView.backgroundColor = [UIColor whiteColor];
        self.bottomBgView.userInteractionEnabled = YES;
        [self addSubview:self.bottomBgView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        cancelBtn.frame = CGRectMake(0 , 0, 60, 40);
        [cancelBtn addTarget:self action:@selector(cancelBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.bottomBgView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        sureBtn.frame = CGRectMake(width - 60, 0, 60, 40);
        [sureBtn addTarget:self action:@selector(sureBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [self.bottomBgView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width, 0.5)];
        line.backgroundColor = DAU_COLOR_SeparotorLine(1.0);
        [self.bottomBgView addSubview:line];
        
        UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, width, kBottomBgViewHeight - 40)];
        picker.showsSelectionIndicator = YES;
        picker.delegate = self;
        picker.dataSource = self;
        self.pickerView = picker;
        [self.bottomBgView addSubview:picker];
    }
    
    return self;
}

#pragma mark - <UIPickerViewDataSource>
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return kMaxValue - kMinValue + 1;
}

#pragma mark - <UIPickerViewDelegate>
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%zd ℃", row + kMinValue];
}

- (void)showInView:(UIView *)view {
    if (self.superview) {
        return;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];

    // 滚动到指定位置
    [self.pickerView selectRow:(self.lowerValue - kMinValue) inComponent:0 animated:NO];
    [self.pickerView selectRow:(self.upperValue - kMinValue) inComponent:1 animated:NO];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.bottomBgView.transform = CGAffineTransformMakeTranslation(0, -kBottomBgViewHeight);
    } completion:^(BOOL finished) {
        ;
    }];
}

- (void)hideWithAnimation:(BOOL)animate {
    CGFloat duration = animate ? 0.2 : 0.0;
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:duration animations:^{
        self.alpha = 0;
        self.bottomBgView.transform = CGAffineTransformMakeTranslation(0, kBottomBgViewHeight);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)dismiss {
    [self hideWithAnimation:YES];
}

- (void)cancelBtnTapped:(UIButton *)button {
    [self hideWithAnimation:YES];
}

- (void)sureBtnTapped:(UIButton *)button {
    NSInteger lower = [self.pickerView selectedRowInComponent:0] + kMinValue;
    NSInteger upper = [self.pickerView selectedRowInComponent:1] + kMinValue;
    
    if ([self.delegate respondsToSelector:@selector(xDSelSecureLineView:lower:upper:)]) {
        [self.delegate xDSelSecureLineView:self lower:MIN(lower, upper) upper:MAX(lower, upper)];
    }
    
    [self hideWithAnimation:YES];
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//
//    //    for (UIButton * button in self.buttonArray) {
//    //        if ([button pointInside:point withEvent:event]) {
//    //            return button;
//    //        }
//    //    }
//
//    UIView * hitView = [super hitTest:point withEvent:event];
//    if ([hitView isKindOfClass:[UIButton class]]) {
//        return hitView;
//    }
//
//    [self hideWithAnimation:YES];
//    return nil;
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self hideWithAnimation:YES];
}

@end
